
import 'package:flutter/material.dart';

class TextFieldAutoFree extends StatelessWidget {
  const TextFieldAutoFree({
    super.key,
    this.controller,
    this.focusNode,
    this.maxLines,
    this.enabled = true,
    this.isCollapsed = true,
    this.contentPadding = const EdgeInsets.all(10),
    this.noBorder = false,
    this.borderRadius = 4.0,
    this.borderside = const BorderSide(),
    this.fieldStyle = const TextStyle(color: Colors.grey, fontSize: 15),
    this.labelText,
    this.hintText,
    this.icon,

    this.useFormText = false,
    this.obscureText = false,
    this.validator
  });

  /// focusNode用于使用代码获取焦点
  final FocusNode? focusNode;
  /// 是否启用
  final bool enabled;
  /// 相当于高度包裹的意思，必须设置为true，不然有默认奇妙的最小高度
  final bool isCollapsed;
  final bool noBorder;
  final TextEditingController? controller;
  final int? maxLines;
  final EdgeInsetsGeometry? contentPadding;
  final BorderSide borderside;
  final TextStyle? fieldStyle;
  final double borderRadius;

  final String? labelText;
  final String? hintText;
  final Widget? icon;

  /// 使用FormTextField
  final bool useFormText;

  /// TextFormField
  final bool obscureText;
  final FormFieldValidator? validator;

  

  @override
  Widget build(BuildContext context) {
    onTapOutside(event) {
      if (focusNode != null) {
        focusNode!.unfocus();
      } else {
        FocusScope.of(context).requestFocus(FocusNode());
      }
    }
    return useFormText ?
      TextFormField(
        focusNode: focusNode,
        controller: controller,
        keyboardType: TextInputType.text,
        enabled: enabled,
        maxLines: maxLines,
        style: fieldStyle,
        decoration: InputDecoration(
          // fillColor: Colors.grey[50],//背景颜色，必须结合filled: true,才有效
          // filled: true,//重点，必须设置为true，fillColor才有效
          labelText: labelText,
          hintText: hintText,
          icon: icon,
          contentPadding: contentPadding,
          isCollapsed: isCollapsed,
          // enabledBorder优先级高于border
          enabledBorder: OutlineInputBorder(
            borderSide: borderside,
          ),
          border:  OutlineInputBorder(
            borderSide: noBorder ?
              BorderSide.none :
              borderside,
            borderRadius: BorderRadius.all(Radius.circular(borderRadius))
          ),
        ),
        onTapOutside: onTapOutside,
      ) :
      TextField(
        focusNode: focusNode,
        controller: controller,
        keyboardType: TextInputType.text,
        enabled: enabled,
        maxLines: maxLines,
        style: fieldStyle,
        decoration: InputDecoration(
          labelText: labelText,
          hintText: hintText,
          icon: icon,
          contentPadding: contentPadding,
          isCollapsed: isCollapsed,
          // enabledBorder优先级高于border
          enabledBorder: OutlineInputBorder(
            borderSide: borderside,
          ),
          border:  OutlineInputBorder(
            borderSide: noBorder ?
              BorderSide.none :
              borderside,
            borderRadius: BorderRadius.all(Radius.circular(borderRadius))
          ),
        ),
        onTapOutside: onTapOutside,
      );
  }
}